<?php

if (!function_exists('udate')) {
    /**
     * [udate  年月日时分秒毫秒]
     * @param  string  $format     [格式化]
     * @param  [type]  $utimestamp [description]
     * @param  integer $precision  [精度]
     * @return [type]              [description]
     */
    function udate($format = 'u', $utimestamp = null, $precision = 10000)
    {
        if (is_null($utimestamp)) {
            $utimestamp = microtime(true);
        }

        $timestamp    = floor($utimestamp);
        $milliseconds = round(($utimestamp - $timestamp) * $precision);

        return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
    }
}

if (!function_exists('checkDateIsValid')) {
    /**
     * 校验日期格式是否正确
     *
     * @param string $date 日期
     * @param string $formats 需要检验的格式数组
     * @return boolean
     */
    function checkDateIsValid($date, $formats = array("Y-m-d", "Y/m/d", "Y/n/j", "Y-n-j"))
    {
        $unixTime = strtotime($date);
        if (!$unixTime) {
            // strtotime转换不对, 日期格式显然不对。
            return false;
        }
        // 校验日期的有效性，只要满足其中一个格式就OK
        foreach ($formats as $format) {
            if (date($format, $unixTime) == $date) {
                return true;
            }
        }

        return false;
    }
}

if (!function_exists('makeRandomNumber')) {
    function makeRandomNumber($length = 32)
    {
        // 密码字符集，可任意添加你需要的字符
        $chars = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h',
            'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's',
            't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D',
            'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O',
            'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
            '0', '1', '2', '3', '4', '5', '6', '7', '8', '9');
        // 在 $chars 中随机取 $length 个数组元素键名
        $keys         = array_rand($chars, $length);
        $randomNumber = '';
        for ($i = 0; $i < $length; $i++) {
            // 将 $length 个数组元素连接成字符串
            $randomNumber .= $chars[$keys[$i]];
        }
        return $randomNumber;
    }
}

if (!function_exists('proxy_http_request')) {
    /**
     * [proxy_http_request 请求转发]
     * @param  [type] $type [description]
     * @param  [type] $url  [description]
     * @param  array  $data [description]
     * @return [type]       [description]
     */
    function proxy_http_request($type, $url, $data = [])
    {
        $ch = curl_init();
        if (strtoupper($type) === 'GET') {
            // 设置选项，包括URL
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch, CURLOPT_HEADER, 0);
        } else if (strtoupper($type) === 'POST') {
            // curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
            // 设置url
            curl_setopt($ch, CURLOPT_URL, $url);
            // TRUE, 将curl_exec()获取的信息以字符串返回, 而不是直接输出
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            // 超时时间
            curl_setopt($ch, CURLOPT_TIMEOUT, 5);
            // curl如果需要进行毫秒超时
            // curl_easy_setopt(curl, CURLOPT_NOSIGNAL,1L);
            // 或者curl_setopt ( $ch,  CURLOPT_NOSIGNAL,true);//支持毫秒级别超时设置
            // 设置发送方式:post
            curl_setopt($ch, CURLOPT_POST, 1);
            // 设置发送数据
            // curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
            curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
        }
        // 执行cURL会话
        $response = curl_exec($ch);
        if (curl_errno($ch)) {
            return curl_error($ch);
            // print curl_error($ch);
        }
        //释放curl句柄
        curl_close($ch);
        // 在这里处理要还给
        // $response = json_decode($response, true);
        return $response;
    }
}

if (!function_exists('image_match')) {
    /**
     * 判断图片大中小
     * @param string 图片的路径
     * @param needSize 需要的图片大小 BIMG大 中MIMG SIMG
     * @return string 图片的路径
     */
    function image_match($href, $needSize = 'SIMG')
    {
        if ($needSize == 'BIMG') {
            if (preg_match('/.*?@(BIMG|bimg)\.(jpg|jpeg|png|gif)$/', $href)) {
                return $href;
            }
        } else if ($needSize == 'MIMG') {
            if (preg_match('/.*?@(MIMG|mimg)\.(jpg|jpeg|png|gif)$/', $href)) {
                return $href;
            }
        } else if ($needSize == 'SIMG') {
            if (preg_match('/.*?@(SIMG|simg)\.(jpg|jpeg|png|gif)$/', $href)) {
                return $href;
            }
        }
    }
}

if (!function_exists('getOnclickEvent')) {
    /**
     * 获取栏目定义或栏目列表onclick事件
     * @param  [type] $tplType [description]
     * @param  [type] $tplID   [description]
     * @param  string $action  [description]
     * @param  string $range   [description]
     * @param  string $href    [description]
     * @return [type]          [description]
     */
    function getOnclickEvent($href = null,$tplType, $tplID = null, $action = 'route', $range = 'area', $area = 'y2')
    {
        $data            = [];
        $data['action']  = $action;
        $data['range']   = $range;
        $data['area']    = $area;
        $data['tplType'] = $tplType;
        $data['tplID']   = $tplID;
        // 链接
        $data['param']['href'] = $href;

        return json_encode($data);
    }
}

if (!function_exists('getMessageOnclickEvent')) {
    /**
     * 获取背景图片的onclick
     * @param  [type] $tplType [description]
     * @param  [type] $tplID   [description]
     * @param  string $action  [description]
     * @param  string $range   [description]
     * @param  string $area    [description]
     * @param  [type] $message [description]
     * @return [type]          [description]
     */
    function getMessageOnclickEvent($message, $tplType = 'metaTpl', $tplID = 'text', $action = 'route', $range = 'area', $area = 'message')
    {
        $data            = [];
        $data['action']  = $action;
        $data['range']   = $range;
        $data['area']    = $area;
        $data['tplType'] = $tplType;
        $data['tplID']   = $tplID;
        // 链接
        $data['param']['message'] = $message;
        return json_encode($data);
    }
}

if (!function_exists('getBgOnclickEvent')) {
    function getBgOnclickEvent($href = '', $tplType = 'metaTpl', $tplID = 'bg', $action = 'route', $range = 'area', $area = 'page')
    {
        $data            = [];
        $data['action']  = $action;
        $data['range']   = $range;
        $data['area']    = $area;
        $data['tplType'] = $tplType;
        $data['tplID']   = $tplID;
        // 链接
        $data['param']['href'] = $href;
        return json_encode($data);
    }
}
